<template>
  <a-button type="primary" @click="onClick">新增</a-button>
  <user-modal
    v-model:open="open"
    v-model:title="title"
    :context-data="propsModalData"
    @save="handleSave"
  />
</template>
<script setup>
import UserModal from './table-new-modal.vue'
import { message } from 'ant-design-vue'
import { addUser } from '@/api/user.js'
import { useDataSourceManager } from './provide-context.js'
import { ref } from 'vue'
/**
 * 全局数据
 */
let { update } = useDataSourceManager()
let open = ref(false)
let title = ref('新增人员')
let propsModalData = ref({})
/**
 * 新增操作
 */
function onClick() {
  open.value = true
  propsModalData.value = {
    btnType: 'add',
  }
}
async function handleSave(data) {
  try {
    // 只有工作状态为实习时，实习日期才能有值
    if (data.userStatus !== 'intern') {
      data.internshipEndDate = null
    }
    await addUser(data)
    await update()
    message.success('新增成功')
  } catch (e) {
    message.error('新增失败')
  }
}
</script>
